package com.flying.decoration;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author 刘鹏飞
 * @ClassName TestDecoraion
 * @Date 2025/2/24 22:42
 * @Created by 刘鹏飞
 * @Description 测试装饰器模式
 * <p>
 * 装饰器模式是GOF23种设计模式中较为常用的一种模式。它可以实现对原有类的包装和装饰，使新的类具有更强的功能
 */
public class TestDecoration {

    public static void main(String[] args) throws FileNotFoundException {

        Phone phone = new Phone("老年机");
        phone.call();

        System.out.println("装饰后");

        SmartPhone smartPhone = new SmartPhone(phone);
        smartPhone.call();



        // IO流体系中大量使用了装饰器模式，让流具有更强的功能、更强的灵活性。比如：
        // 显然BufferedInputStream装饰了原有的FileInputStream，让普通的FileInputStream也具备了缓存功能，提高了效率
        FileInputStream fis = new FileInputStream("demo.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
    }




}

class Phone {
     final String name;

    public Phone(String name) {
        this.name = name;
    }


    public void call() {
        System.out.println(this.name + ",可以打电话");
    }
}


class SmartPhone {
     final Phone phone;

    public SmartPhone(Phone phone) {
        this.phone = phone;
    }

    public void call(){
        this.phone.call();
        System.out.println(this.phone.name+",可以打视频电话");
    }

}